﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Money.Interfaces;
using One.Framework;
using Money.Data;

namespace Money.Services
{
    public class MemorizedTransactionService : IMemorizedTransactionService
    {
        private Money.Data.MoneyManagerDataContext context;

        public MemorizedTransactionService()
        {
            context = new Money.Data.MoneyManagerDataContext();
        }

        public void CreateMemorizedTransaction(Money.Data.MemorizedTransaction item)
        {
            try
            {
                using (MoneyManagerDataContext contx = new MoneyManagerDataContext())
                {
                    item.CreationOn = DateTime.Now;
                    if (item.Account1 != null)
                        item.Account1 = contx.Accounts.SingleOrDefault(a => a.AccountId == item.Account1.AccountId);
                    contx.MemorizedTransactions.InsertOnSubmit(item);
                    contx.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot create memorized transaction");
            }
        }

        public void DeleteMemorizedTransaction(int id)
        {
            try
            {
                MemorizedTransaction item = GetMemorizedTransactionById(id);
                context.MemorizedTransactions.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot delete memorized transaction");
            }
        }

        public void UpdateMemorizedTransaction(Money.Data.MemorizedTransaction item)
        {
            try
            {
                using (MoneyManagerDataContext contx = new MoneyManagerDataContext())
                {
                    item.ModifiedOn = DateTime.Now;
                    item.Account = contx.Accounts.SingleOrDefault(a => a.AccountId == item.AccountId);
                    item.Category = contx.Categories.SingleOrDefault(c => c.Id == item.CategoryId);
                    item.Ref = contx.Refs.SingleOrDefault(r => r.RefId == item.RefId);
                    item.Class = contx.Classes.SingleOrDefault(c => c.Id == item.ClassId);
                    if (item.Account1 != null)
                        item.Account1 = contx.Accounts.SingleOrDefault(a => a.AccountId == item.Account1.AccountId);
                    contx.MemorizedTransactions.Attach(item);
                    contx.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, item);

                    contx.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot update memorized transaction");
            }
        }

        public Money.Data.MemorizedTransaction GetMemorizedTransactionById(int id)
        {
            try
            {
                return context.MemorizedTransactions.Where(t => t.Id == id).SingleOrDefault<MemorizedTransaction>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get memorized transaction");
            }
        }

        public List<Money.Data.MemorizedTransaction> GetMemorizedTransactions(int userId, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                var query = from u in context.MemorizedTransactions
                            where u.Account.UserId == userId
                            select u;
                return query.OrderByPaging<MemorizedTransaction>(orderBy, orderDirection, page, pageSize, out totalRecords).ToList<MemorizedTransaction>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get memorized transactions");
            }
        }

        public List<Money.Data.MemorizedTransaction> GetMemorizedTransactions(int accountId, int transactionType, string payer, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                var query = from u in context.MemorizedTransactions
                            where u.AccountId == accountId && u.TransactionType == transactionType && u.PayMan == payer
                            select u;
                return query.OrderByPaging<MemorizedTransaction>(orderBy, orderDirection, page, pageSize, out totalRecords).ToList<MemorizedTransaction>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get memorized transactions");
            }
        }
    }
}
